package com.letv.so.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.letv.so.service.QueryDataService;
import com.letv.so.util.NumberUtil;
import com.letv.so.util.StringUtil;

/**
 * 
 */
public abstract class AbstractQueryDataServlet extends BaseServlet {

	private static final long serialVersionUID = -4530700506509711878L;
	private Logger logger = Logger.getLogger(AbstractQueryDataServlet.class);

	protected QueryDataService queryDataService;

	public void init() throws ServletException {
		super.init();
		queryDataService = (QueryDataService) wac.getBean("queryDataService");
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long beginTime = System.currentTimeMillis();
		String rawInfo = request.getRequestURI() + "?" + request.getQueryString();
//		logger.debug("detail req{" + rawInfo + "},ts=" + beginTime);
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		Map<String, Object> ruleMap = new HashMap<String, Object>();

		String soKey = request.getParameter("sk");// 关键词
		String op = request.getParameter("op");// 关键词
		int pn = NumberUtil.getInt(request.getParameter("pn"), 0);// 第几页
		int ps = NumberUtil.getInt(request.getParameter("ps"), 50);// 每页多少个
		String subsrc = request.getParameter("subsrc");
		String src = request.getParameter("src");
		String vrsAid = request.getParameter("vrsAid");
		String sid = request.getParameter("uniqueId");
		
		ruleMap.put("soKey", soKey);
		ruleMap.put("op", op);
		ruleMap.put("pn", pn);
		ruleMap.put("ps", ps);
		ruleMap.put("subsrc", subsrc);
		ruleMap.put("src", src);
		ruleMap.put("vrsAid", vrsAid);
		if (StringUtils.isBlank(sid)) {
	        sid = String.valueOf(System.currentTimeMillis());
        }
		ruleMap.put("sid", sid);
		
		int videoListSize = NumberUtil.getInt(request.getParameter("vls"), 0);// 专辑关联的视频数量
		ruleMap.put("videoListSize", videoListSize);

		String result = query(ruleMap);

		String callback = request.getParameter("callback");// 回调
		if (!StringUtil.isBlank(callback)) {
			result = callback + "(" + result + ")";
		}

		long markTime = System.currentTimeMillis();
		logger.info(sid+" ,detail req{" + rawInfo + "} ---cost time:[" + (markTime - beginTime)+"]");
		print(request, response, result);
	}

	public void print(HttpServletRequest request, HttpServletResponse response, String result) throws ServletException, IOException {
		String encode = request.getParameter("encode");
		encode = StringUtils.isBlank(encode) ? "UTF-8" : encode;
		response.setCharacterEncoding(encode);
		response.setContentType("text/javascript; charset=" + encode);
		this.printNoCache(response, result);
	}

	protected abstract String query(Map<String, Object> rule);
}
